<template>
  <div id="aCoursesList" class="bg-fa of">

    <!-- /课程详情 开始 -->
    <section class="container">
      <section class="path-wrap txtOf hLh30">
        <a href="#" title class="c-999 fsize14">首页</a>
        \
        <a href="#" title class="c-999 fsize14">{{questionInfo.subjectParentName}}</a>
        \
        <span class="c-333 fsize14">{{questionInfo.subjectName}}</span>
      </section>
      <div>
        <article class="c-v-pic-wrap" style="height: 357px;">
          <!-- 文章头部 -->
              <header class="entry-header">
                  <!-- 标题输出 -->
                  <span class="commentTitle">{{questionInfo.title}}</span>
                  <hr>
                  <hr>
                  <br>
              </header>
              <!-- 正文输出 -->
              <div class="entry-content">
                  <p v-html="questionInfo.content">{{questionInfo.content}}</p>
              </div>
              <div class="breadcrumbs">
                  <div id="crumbs">最后更新时间：{{questionInfo.updateTime}}</div>
              </div>
        </article>
        <div class="clear"></div>
      </div>
      <!-- 问答评论 -->
      <div class="mt50 commentHtml"><div>
            <h6 class="c-c-content c-infor-title" id="i-art-comment">
              <span class="commentTitle">问答评论</span>
            </h6>
            <section class="lh-bj-list pr mt20 replyhtml">
              <ul>
                <li class="unBr">
                  <aside class="noter-pic">
                    <img width="50" height="50" class="picImg" src="~/assets/img/loading.gif">
                    </aside>
                  <div class="of">
                    <section class="n-reply-wrap">
                      <fieldset>
                        <textarea name="" v-model="comment.content" placeholder="输入您要评论的文字" id="commentContent"></textarea>
                      </fieldset>
                      <p class="of mt5 tar pl10 pr10">
                        <span class="fl "><tt class="c-red commentContentmeg" style="display: none;"></tt></span>
                        <input type="button" @click="addCourseComment()" value="发布" class="lh-reply-btn">
                      </p>
                    </section>
                  </div>
                </li>
              </ul>
            </section>
            <section class="">
                <section class="question-list lh-bj-list pr">
                  <ul class="pr10">
                    <li v-for="(comment,index) in data.items" v-bind:key="index">
                        <aside class="noter-pic">
                          <img width="50" height="50" class="picImg" :src="comment.avatar">
                          </aside>
                        <div class="of">
                          <span class="fl">
                          <font class="fsize12 c-blue">
                            {{comment.nickname}}</font>
                          <font class="fsize12 c-999 ml5">评论：</font>
                          </span>
                        </div>
                        <div class="noter-txt mt5">
                          <p>{{comment.content}}</p>
                        </div>
                        <div class="of mt5">
                          <div style="text-align: right;" v-if="comment.memberId == memberId">
                            <input type="button" @click="deleteComment(comment.id)" value="删除" class="lh-reply-btn">
                          </div>
                        </div>

                        <div class="of mt5">
                          <span class="fr"><font class="fsize12 c-999 ml5">{{comment.gmtCreate}}</font></span>
                        </div>
                      </li>
                    </ul>
                </section>
              </section>

              <!-- 公共分页 开始 -->
                      <div>
          <div class="paging" v-if="data.total > 0">
          <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
          <a
            :class="{undisable: !data.hasPrevious}"
            href="#"
            title="首页"
            @click.prevent="gotoPage(1)">首页</a>

          <a
            :class="{undisable: !data.hasPrevious}"
            href="#"
            title="前一页"
            @click.prevent="gotoPage(data.current-1)">&lt;</a>

          <a
            v-for="page in data.pages"
            :key="page"
            :class="{current: data.current == page, undisable: data.current == page}"
            :title="'第'+page+'页'"
            href="#"
            @click.prevent="gotoPage(page)">{{ page }}</a>

          <a
            :class="{undisable: !data.hasNext}"
            href="#"
            title="后一页"
            @click.prevent="gotoPage(data.current+1)">&gt;</a>

          <a
            :class="{undisable: !data.hasNext}"
            href="#"
            title="末页"
            @click.prevent="gotoPage(data.pages)">末页</a>

          <div class="clear"/>
        </div>
        </div>
              <!-- 公共分页 结束 -->
            </div>
          </div>

    </section>
    <!-- /课程详情 结束 -->
  </div>
</template>

<script>
import question from '@/api/question'
//引入调用collect.js文件
// import collectApi from '@/api/collect'
//引入调用order.js文件
//引入调用comment.js文件
import commentApi from '@/api/comment'
//引入调用js-cookie
import cookie from 'js-cookie'

export default {
  asyncData({params, error}) {
    return {questionId: params.qid}
  },
  data() {
      return {
        data:{}, // 评论分页所有信息
        // 评论
        comment:{
          content:'',
          courseId:'',
          nickname:'',
          avatar:'',
        },
        questionInfo: {},// 问答信息
        questionId: '',  // 问答ID
        memberId: '' // 用户ID
      }
  },
  created() {// 页面渲染之前执行
    this.initCourseInfo()
    this.getCommonentList()
    this.getMemberId()
  },
  methods: {
    initCourseInfo() {
      question.getQuestionInfo(this.questionId)
        .then(response => {
              this.questionInfo = response.data.data.questionInfo
        })
    },
    // =============================评论==========================================
    getCommonentList() {
      commentApi.getPageList(1, 4, this.questionId)
        .then(response => {
          this.data = response.data.data
        })
    },
    // 添加评论
    addCourseComment() {
      // 不为空
      if (this.comment.content === '') {
          this.$message({
              type: 'warn',
              message: '内容不能为空'
            })
      } else {
          const token = cookie.get('tax_token')
          if (token) {
            this.comment.questionId = this.questionInfo.id
            commentApi.addCourseComment(this.comment)
              .then(response => {
                this.$message({
                  type: 'success',
                  message: '发布评论成功'
                })
                this.getCommonentList()  
                this.comment = {}
              })
              
          } else {
            this.$message({
              type: 'warn',
              message: '请先登录再进行下一步操作'
            })
            // vue 路由跳转
            this.$router.push({path: '/login'})
          }
      }
    },
    // 根据页面跳转
    gotoPage(page=1) {
      commentApi.getPageList(page, 4, this.questionInfo)
        .then(response => {
          this.data = response.data.data
        })
    },
    //删除评论
    deleteComment(commentId){
      this.$confirm('确认要删除当前评论吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return commentApi.deleteComment(commentId)
      }).then((response) => {
        this.$message({
          type: 'success',
          message: '删除成功✌',
        })
        this.getCommonentList()
      }).catch(error => {
        if (error === 'cancel') {
          this.$message({
            message: '取消删除'
          })
        }
      })
    },
    // 获取Memberid
    getMemberId() {
      commentApi.getMemberId()
        .then(response => {
          this.memberId = response.data.data.memberId
        })
    }
  }
};
</script>
<style scoped lang="less">
    .site-content {
        position: relative;
        .site-main {
            padding: 80px 0 0 0;
        }
    }
    #article-menus{
        position: sticky;
        top: 0;
        box-shadow: 0 2px 6px rgba(0, 0, 0, .1);
        border-radius: 3px;
        padding: 15px;
        width: 300px;
        transform: translateX(-120%) translateY(150px);
        font-size: 14px;
    }
    article.hentry {
        .entry-header {
            .entry-title {
                font-size: 23px;
                font-weight: 600;
                color: #737373;
                margin: 0.67em 0;

                &:before {
                    content: "#";
                    margin-right: 6px;
                    color: #d82e16;
                    font-size: 20px;
                    font-weight: 600;
                }
            }

            hr {
                height: 1px;
                border: 0;
                background: #EFEFEF;
                margin: 15px 0;
            }

            .breadcrumbs {
                font-size: 14px;
                color: #D2D2D2;
                text-decoration: none;
                margin-bottom: 30px;
            }
        }

        .entry-content {}

        footer.post-footer {
            width: 100%;
            padding: 20px 10px;
            margin-top: 30px;
            height: 65px;
            position: relative;
            i{
                font-size: 18px;
                margin-right: 5px;
            }
            .post-like {
                float: right;
                margin: 7px 0 0 20px;
            }

            .post-share {
                float: right;
                list-style: none;
                margin-right: 20px;
            }

            .donate {
                float: left;
                line-height: 36px;
                border-radius: 100%;
                -webkit-border-radius: 100%;
                -moz-border-radius: 100%;
                border: 1px solid #2B2B2B;
                &:hover{
                    border: 1px solid goldenrod;
                    span{
                        color: goldenrod;
                    }
                }
                span {
                    color: #2B2B2B;
                    padding: 10px;
                    position: relative;
                    cursor: pointer;
                }

                .donate_inner {
                    display: none;
                    margin: 0;
                    list-style: none;
                    position: absolute;
                    left: 80px;
                    top: -40px;
                    background: #FFF;
                    padding: 10px;
                    border: 1px solid #ddd;
                    box-shadow: 0 2px 6px rgba(0, 0, 0, .08);
                    border-radius: 3px;
                    &.show{
                        display: block;
                    }
                    li {
                        float: left;
                    }

                    img {
                        width: 100px;
                    }
                    p {
                        text-align: center;
                        font-size: 15px;
                        color: #D2D2D2;
                        line-height: 1rem;
                    }
                }

                .donate_inner:after, .donate_inner:before {
                    content: "";
                    position: absolute;
                    left: 0;
                    bottom: 45%;
                    margin-left: -8px;
                    border-top: 8px solid transparent;
                    border-bottom: 8px solid transparent;
                    border-right: 8px solid #fff;
                }

                .donate_inner:before {
                    left: -1px;
                    border-right: 8px solid #ddd;
                }

            }

            .post-tags {
                margin: 7px 0 0 20px;
                float: left;
                text-transform: uppercase;
                a:hover{
                    color: #ff6d6d;
                }
            }
        }
        .open-message {
            margin: 50px 0;
            position: relative;
            background: #2B2B2B;
            padding: 10px 30px;
            border-radius: 3px;
            font-size: 14px;
            color: #fff;
            &:after {
                content: "";
                border-left: 10px solid transparent;
                border-right: 10px solid transparent;
                border-bottom: 10px solid #2B2B2B;
                position: absolute;
                top: -8px;
                left: 48%;
            }
            p {
                margin: 10px 0;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            a {
                color: #A0DAD0;
                padding: 0 5px;
            }
        }
    }
</style>